package com.enjava;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import libreria.Editorial;
import libreria.Libro;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RequestScoped
public class MiEjemplo {
	private Log log = LogFactory.getLog(MiEjemplo.class);

	Binding binding;
	
	@Autowired
	public Binding getBinding() {
		return binding;
	}

	public void setBinding(Binding binding) {
		this.binding = binding;
	}

	EntityManagerFactory entityManagerFactory;

	
	@Autowired()
	public EntityManagerFactory getEntityManagerFactory() {
	
		return entityManagerFactory;
	}

	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}
	
	@PersistenceContext(name="em", type=PersistenceContextType.EXTENDED)
	private EntityManager entityManager;

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
    public void escucha(AjaxBehaviorEvent event) {
        System.out.println("listener"+event.getComponent().getClass().getName());
    }

	
	public List<Libro> getItems(){
		return entityManager.createQuery("from Libro").getResultList();
	}
	public List<Libro> getLibros(){
		return entityManagerFactory.createEntityManager().createQuery("from Libro").getResultList();
	}

	public String enviaFormulario(){
		log.error("enviando /recibiendo formulario");
		return null;
	}
	@Transactional
	public String enviaFormulario(Libro libro){
		entityManager.merge(libro);
		entityManagerFactory.createEntityManager().merge(libro.getEditorial());
		log.error("enviando /recibiendo formulario con libro:"+libro.getAutores());
		return null;
	}
	@Transactional
	public String editaLibro(Libro libro){
		
		entityManager.refresh(libro);
		this.libro=libro;
		log.error("enviando /recibiendo formulario con libro:"+libro.getAutores());
		return "libro?faces-redirect=true";
	}
	Editorial editorial;
	
	public Editorial getEditorial() {
		return editorial;
	}

	public void setEditorial(Editorial editorial) {
		this.editorial = editorial;
	}

	public String creaLibro(){
		libro=new Libro();
		iniciaEditoriales();
		return "libro?faces-redirect=true";
	}


	@PostConstruct
	private void iniciaEditoriales() {

		editoriales=new ArrayList<SelectItem>();
		List<Editorial> edits = entityManager.createQuery("from Editorial").getResultList();
		for(Editorial edit:edits){
			editoriales.add(new SelectItem(edit, edit.getNombre()));
		}
	}
	Libro libro;
	List<SelectItem> editoriales;
	
	
	
	public List<SelectItem> getEditoriales() {
		return editoriales;
	}

	public Libro getLibro() {
		return libro;
	}

	public void setLibro(Libro libro) {
		this.libro = libro;
	}
	
	
	
	@Transactional()
	public String guardaLibro(){
		binding.getNombre().setStyle("background-color:red;");
        FacesMessage message = new FacesMessage("Invalid password length");
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(binding.getNombre().getClientId(context), message);

		return null;
//		if(libro.getId()==null)
//			entityManager.persist(libro);
//		else
//			entityManager.merge(libro);
//			
//		return "libros?faces-redirect=true";
	}
	
}
